import {hex2Buf} from "@/utils/appCommon/waveMath/hexbuf";

export function aeRawWave2List(hex:string) {
  const buf = hex2Buf(hex)
  const length = buf.byteLength
  var dataView = new DataView(buf);
  let arr = []
  for(let i=0; i<length; i += 2){
    const thisV = dataView.getInt16(i,true)
    arr.push(thisV)
  }
  //console.log(arr)
  return arr
}


// 针对m1
// (AD值*Scale*10V)/(30000)
// (item * ae_enlarge_value * 10 * 1000 / 30000) / ae_gain_value

// u3h的
// 波形计算公式：((data / 30000.0f) * 2.048) / Scale / 10.0 / 10;  其中：Scale - 电路放大系数

export function getAeWaveY(productKey, waveList,ae_gain_value,ae_enlarge_value) {
  if(ae_enlarge_value == 0){
    ae_enlarge_value = 1
  }
  let max = 0
  let min = 0
  let ret = []

  let muliply1 = 2
  if(ae_gain_value == 20){
    muliply1 = 2
  }else if(ae_gain_value == 50){
    muliply1 = 5
  }else if(ae_gain_value == 100){
    muliply1 = 10
  }

  waveList.forEach((item,index) => {
    let v2 = 0
    if(productKey.toUpperCase() == "RAEM1" || productKey.toUpperCase() == "M1V1" || productKey.toUpperCase() == "M1V2"){
      v2 = (item * ae_enlarge_value * 10 * 1000 / 30000) / ae_gain_value
    }else {
      v2 = item*muliply1*1000/32768
    }
    if(index==0){
      max = v2
      min = v2
    }else{
      if(v2 > max){
        max = v2
      }
      if(v2 < min){
        min = v2
      }
    }
    ret.push(v2)
  })
  return {
    list: ret,
    unit : 'mV',
    max: max,
    min: min,
  }
}

export function getAeWaveX(productKey,waveList,ae_measure_speed) {
  let length = waveList.length
  const space = 1000/ae_measure_speed
  let ret = []
  for(let i=0; i< length; i++){
    ret.push(space * i)
  }
  return {
    list: ret,
    unit : 'μs'
  }
}

export function getAEFFTWaveX(fftWaveList, ae_measure_speed) {
  ae_measure_speed = Number(ae_measure_speed)  //k/s *1000
  let length = fftWaveList.length
  let ret = []
  if(ae_measure_speed <1){
    ae_measure_speed = 1
  }

  const space = ae_measure_speed / (2*length)
  for(let i=0; i< length; i++){
    ret.push(space * i)
  }
  return {
    list: ret,
    unit : 'kHz'
  }
}


